package com.example.friendfindertwo.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import com.example.friendfindertwo.LoginActivity;
import com.example.friendfindertwo.R;
import com.example.friendfindertwo.adapter.FriendRequestListAdapter;

public class FriendRequestFragment extends ListFragment {

	public static final String ARG_PAGE = "ARG_PAGE";
	private int mPage;
	FriendRequestListAdapter adapter = null;
	List<String> friends = new ArrayList<String>();

	public static FriendRequestFragment create(int page) {
		Bundle args = new Bundle();
		args.putInt(ARG_PAGE, page);
		FriendRequestFragment fragment = new FriendRequestFragment();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPage = getArguments().getInt(ARG_PAGE);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.friend_request_page, container,
				false);

		new FriendRequestDownloader().execute();

		return view;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
		menu.setHeaderTitle(friends.get(aInfo.position));
		menu.add(1, 1, 1, "Accept the request");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		// Implements our logic
		if (getUserVisibleHint()) {
	        // context menu logic
	       
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		Log.d("friends", "accept request" +
				" : " + friends.get(info.position));
		LoginActivity.dataBaseObject.acceptRequest((String) friends
				.get(info.position));

		 return true;
	    }
	    return false;	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		registerForContextMenu(getListView());
	}

	private class FriendRequestDownloader extends
			AsyncTask<Void, List<String>, List<String>> {
		String[] request;
		ProgressDialog myPd_ring;

		@Override
		protected void onPreExecute() {
			myPd_ring = new ProgressDialog(getActivity());
			myPd_ring.setMessage("loading requests");
			myPd_ring.show();
		}

		@Override
		protected List<String> doInBackground(Void... params) {
			request = LoginActivity.dataBaseObject.getRequest();
			friends = new ArrayList<String>();
			for (int i = 0; i < request.length; i++) {
				friends.add(request[i]);
			}
			return friends;
		}

		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			myPd_ring.dismiss();
			if (getActivity() != null) {
				adapter = new FriendRequestListAdapter(getActivity(), result);
				setListAdapter(adapter);
			}
		}

	}
}
